package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Hosregister;
import com.kkb.service.RegistrationService;
import com.kkb.vo.RegistrationQueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@Controller
@RequestMapping("registration")
@ResponseBody
public class RegistrationController {

    @Resource
    private RegistrationService registrationService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Hosregister> queryByPage(@RequestParam(defaultValue = "1") Integer realPageNum, @RequestParam(defaultValue = "5") Integer pageSize, RegistrationQueryVO vo) {
        PageInfo<Hosregister> info = registrationService.queryByPage(realPageNum, pageSize, vo);
        return new ResultVO<>(info);
    }


    @RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
    public ResultVO<Hosregister> queryById(@PathVariable("id") Integer id) {
        Hosregister hosregister = registrationService.queryById(id);
        ResultVO<Hosregister> hosregisterResultVO = new ResultVO<>(hosregister);
        return hosregisterResultVO;
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public ResultVO<Hosregister> updateRegister(@PathVariable("id") Integer id, Hosregister hosregister) {
        hosregister.setId(id);
        int i = registrationService.updateRegister(hosregister);
        if (i == 1) {
            return new ResultVO<Hosregister>();
        }
        if (i == -1) {
            return new ResultVO<Hosregister>(201, "医生名与所属科室不匹配！");
        }
        return new ResultVO<Hosregister>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public ResultVO<Hosregister> deleteRegister(@PathVariable("id") Integer id) {
        int i = registrationService.deleteRegister(id);
        if (i == 1) {
            return new ResultVO<Hosregister>();
        }
        return new ResultVO<Hosregister>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "/deleteAll/{ids}", method = RequestMethod.DELETE)
    public ResultVO<Hosregister> deleteRegister(@PathVariable("ids") ArrayList<Integer> ids) {
        int i = registrationService.deleteAllRegister(ids);
        if (i >= 1) {
            return new ResultVO<Hosregister>();
        }
        if (i == -1) {
            return new ResultVO<Hosregister>(201, "选中的记录中存在不能退号的记录");
        }
        return new ResultVO<Hosregister>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Hosregister> addRegister(Hosregister hosregister) {
        int i = registrationService.addRegister(hosregister);
        if (i == 1) {
            return new ResultVO<Hosregister>();
        }
        if (i == -1) {
            return new ResultVO<Hosregister>(201, "医生名与所属科室不匹配！");
        }
        return new ResultVO<Hosregister>(500, "服务器内部异常，请稍后再试！");
    }
}
